print("################################");
print("##Gameweb User Script Started###");
print("################################");
require( "mysql" )
require("cryptopp")
require("gatekeeper")  
local host = "127.0.0.1"
local username = ""
local password = ""
local db = ""
AddCSLuaFile( "autorun/functions.lua" )
AddCSLuaFile( "autorun/client/register.lua" )
local Hash = "fgfdghfgkuilu"
function F4( ply )
ply:ConCommand( "help_menu" )
end
hook.Add("ShowSpare2", "f4popup", F4)

function BanTable()
	local db, error = mysql.connect(host, username, password, db)   
	if (db == 0) then
		print(tostring(error) .. "\n")
		return
	end  
	Bans = mysql.query(db, "SELECT SteamID, Bantime FROM bans");
	if Bans then
		local I = 1
		local BanLeng = #Bans or 0
		while I <= BanLeng do
			if tonumber(Bans[I][2]) < os.time() then
				local SteamID = Bans[I][1]
				mysql.query(db,"DELETE FROM bans WHERE SteamID='".. SteamID .."'")
				Bans = mysql.query(db, "SELECT SteamID, Bantime FROM bans");
			end
			I = I + 1
		end
	end
	local succ, error = mysql.disconnect(db)
end
timer.Create( "BanTable", 10, 0, BanTable)
BanTable()

function CheckForValue(Table,String,Value)
if Table then
	local Leng = #Table or 0
	local I = 1
	local Found = false
	while I <= Leng do
		if Table[I][Value] == String then
			Found = true
			return I	
		end
		I = I + 1
	end
	if not Found then
		return false
	end
end
return false
end

function Advert()
timer.Create( "Advert", 300, 0, function()
for k , v in ipairs(player.GetAll()) do
	v:PrintMessage( HUD_PRINTTALK, "Advert: This server is running Gameweb Admin Tool")
end
end)
timer.Create( "Advert2", 120, 0, function()
for k , v in ipairs(player.GetAll()) do
	v:PrintMessage( HUD_PRINTTALK, "Press F4 to open Credits/Ban Request Menu")
end
end)
end
Advert()

function AddUser(ply, con, args)
	checksum = args[3] or ""
	if checksum == "fgfdghfgkuilu" then
	Pass = args[2] or ""
	Pass = crypto.md5(Pass)
	Pass = string.lower(Pass)
	Uname = args[1] or ""
	local db, error2 = mysql.connect(host, username, password, db)  
	if (db == 0) then print(tostring(error2) .. "\n") return end  
	insert, isok, error = mysql.query(db, "INSERT INTO users (SteamID,Status,Nick,Username,Password,Kills,Deaths,Banmarks) VALUES ('".. ply:SteamID() .."',0,'".. ply:Nick() .."','".. args[1] .."','".. Pass .."',0,0,0)");
	if (!isok) then
		print(tostring(error) .. "\n");
	else
	end
	local succ, error = mysql.disconnect(db)
	if (not succ) then
	print( error );
	ply:PrintMessage( HUD_PRINTTALK, "Registration Failed!");
	else
	ply:PrintMessage( HUD_PRINTTALK, "You have been registrated on our site!");
	GamewebTitle(ply);
	end
	end
end
concommand.Add("adduserdb", AddUser)
function AddKill(ply)
	local db, error2 = mysql.connect(host, username, password, db)  
	if (db == 0) then print(tostring(error2) .. "\n") return end  
	insert, isok, error = mysql.query(db, "UPDATE users SET Kills=Kills+1 WHERE SteamID='".. ply:SteamID() .."'");
	if (!isok) then
		print(tostring(error) .. "\n");
	end
	local succ, error = mysql.disconnect(db)
	if (not succ) then
	print( error );
	end
end
function AddDeath(ply)
	local db, error2 = mysql.connect(host, username, password, db)  
	if (db == 0) then print(tostring(error2) .. "\n") return end  
	insert, isok, error = mysql.query(db, "UPDATE users SET Deaths=Deaths+1 WHERE SteamID='".. ply:SteamID() .."'");
	if (!isok) then
		print(tostring(error) .. "\n");
	end
	local succ, error = mysql.disconnect(db)
	if (not succ) then
	print( error );
	end
end

function IsGamewebAdmin(ply)

	local db, error = mysql.connect(host, username, password, db)  
	if (db == 0) then print(tostring(error) .. "\n") return end  
	
	Admin = mysql.query(db, "SELECT Status FROM users WHERE SteamID = '".. ply:SteamID().."'");
	
	local succ, error = mysql.disconnect(db)
	if (not succ) then
		print( error );
	end
	if Admin[1][1] == "2" or Admin[1][1] == "3" then
		return true
	elseif Admin[1] == "2" or Admin[1] == "3" then
		return true
	else
		return false
	end
end
function IsGamewebOwner(ply)

	local db, error = mysql.connect(host, username, password, db)  
	if (db == 0) then print(tostring(error) .. "\n") return end  
	
	Admin = mysql.query(db, "SELECT Status FROM users WHERE SteamID = '".. ply:SteamID().."'");
	
	local succ, error = mysql.disconnect(db)
	if (not succ) then
		print( error );
	end
	if Admin[1][1] == "3" then
		return true
	else
		return false
	end

end
function SetAdmin(ply)

	local db, error = mysql.connect(host, username, password, db)  
	if (db == 0) then print(tostring(error) .. "\n") return end  
	
	Admin = mysql.query(db, "UPDATE users SET Status=2 WHERE SteamID='".. ply:SteamID() .."'");
	
	local succ, error = mysql.disconnect(db)
	if (not succ) then
		print( error );
	end
end
function RemoveAdmin(ply)
	local db, error = mysql.connect(host, username, password, db)  
	if (db == 0) then print(tostring(error) .. "\n") return end  
	
	Admin = mysql.query(db, "UPDATE users SET Status=1 WHERE SteamID='".. ply:SteamID() .."'");
	
	local succ, error = mysql.disconnect(db)
	if (not succ) then
		print( error );
	end
end
function IsGamewebVip(ply)

	local db, error = mysql.connect(host, username, password, db)  
	if (db == 0) then print(tostring(error) .. "\n") return end  
	
		Admin = mysql.query(db, "SELECT Status FROM users WHERE SteamID = '".. ply:SteamID().."'");
	
	local succ, error = mysql.disconnect(db)
	if (not succ) then
		print( error );
	end
	if Admin[1][1] == "1" or Admin[1][1] == "2" or Admin[1][1] == "3" then
		return true
	else
		return false
	end

end
function SetVip(ply)

	local db, error = mysql.connect(host, username, password, db)  
	if (db == 0) then print(tostring(error) .. "\n") return end  
	
	Admin = mysql.query(db, "UPDATE users SET Status=1 WHERE SteamID='".. ply:SteamID() .."'");
	
	local succ, error = mysql.disconnect(db)
	if (not succ) then
		print( error );
	end
end
function BanMark(ply)

	local db, error = mysql.connect(host, username, password, db)  
	if (db == 0) then print(tostring(error) .. "\n") return end  
	
	Banmark = mysql.query(db, "UPDATE users SET Banmarks= Banmarks+1 WHERE SteamID='".. ply:SteamID() .."'");
	
	local succ, error = mysql.disconnect(db)
	if (not succ) then
		print( error );
	end
end
function BanRequest(ply, con, args)

	local db, error = mysql.connect(host, username, password, db)  
	if (db == 0) then print(tostring(error) .. "\n") return end  
	Ban, sok, error = mysql.query(db, "INSERT INTO `banrequests` (`id`, `Poster`, `On`, `Reason`, `SteamID`) VALUES (NULL, '".. ply:Nick() .."', '".. args[1] .."', '".. args[2] .."', '".. args[3] .."')")
	if (!sok) then
		print(tostring(error) .. "\n");
	end
	local succ, error = mysql.disconnect(db)
	if (not succ) then
		print( error );
	end
end
concommand.Add("banrequest", BanRequest)
function GamewebTitle(ply)

	local db, error = mysql.connect(host, username, password, db)  
	if (db == 0) then print(tostring(error) .. "\n") return end  
	
	Admin = mysql.query(db, "SELECT Status FROM users WHERE SteamID = '".. ply:SteamID().."'");
	
	local succ, error = mysql.disconnect(db)
	if (not succ) then
	print( error );
	end
			if Admin[1][1] == "0" then
				return "Guest"
			elseif Admin[1][1] == "1" then
				return "VIP"
			elseif Admin[1][1] == "2" then
				return "Admin"
			elseif Admin[1][1] == "3" then
				return "Owner"
			end

end
function PlyKD(ply)
		local db, error = mysql.connect(host, username, password, db)  
		if (db == 0) then print(tostring(error) .. "\n") return end  
		
		local Kills = mysql.query(db, "SELECT Kills FROM users WHERE SteamID = '" .. ply:SteamID().. "'")  
		local Deaths = mysql.query(db, "SELECT Deaths FROM users WHERE SteamID = '" .. ply:SteamID().. "'")
		local succ, error = mysql.disconnect(db)
		
		if (not succ) then
			print( error );
		end
		local K = Kills[1][1]
		local D = Deaths[1][1]
		KD = K/D
		if K == 0 or D == 0 then KD = 0 end
		return "Your K/D is: ".. KD			
end
local SECONDS_IN_A_MONTH	= 2629743.83;
local SECONDS_IN_A_DAY		= 86400;
local SECONDS_IN_A_HOUR		= 3600;
local SECONDS_IN_A_MINUTE	= 60;

local function FormatSeconds( s )

	local mod;
	local months = math.floor( s / SECONDS_IN_A_MONTH );
	mod = s % SECONDS_IN_A_MONTH;
	local days = math.floor( mod / SECONDS_IN_A_DAY );
	mod = mod % SECONDS_IN_A_DAY;
	local hours = math.floor( mod / SECONDS_IN_A_HOUR );
	mod = mod % SECONDS_IN_A_HOUR;
	local minutes = math.floor( mod / SECONDS_IN_A_MINUTE );
	mod = mod % SECONDS_IN_A_MINUTE;
	local seconds = math.floor( mod );
	
	return "Your banned! The ban expires in ".. days .. " day(s) " .. hours .. " hour(s) " .. minutes .. " minute(s)";

end
function isBanned(user, pass, steam, ip)
		local Banned = CheckForValue(Bans,steam,1)
		if Banned then
			Value = tonumber(Bans[Banned][2])
			if Value > os.time() then
				local Time = Value - os.time()
				return {false, FormatSeconds(Time)} 
			elseif Value == 0 then
				return {false, "You are permabaned if you want to get unbanned contact server owner!"} 
			elseif Value < os.time() then
				mysql.query(db, "DELETE FROM bans WHERE SteamID = '".. steam .."'");
			else
				--Nothing here!
			end
		end
end
hook.Add("PlayerPasswordAuth", "isBanned", isBanned)

function GamewebBan(ply,t,Time,Reason)
	if IsGamewebAdmin(ply) == true then
		if tonumber(Time) == 0 then
			Bantime = 0
		else
			Bantime = os.time()+(tonumber(Time)*60)
		end
		local db, error2 = mysql.connect(host, username, password, db)  
		if (db == 0) then print(tostring(error2) .. "\n") return end  
			insert, isok, error = mysql.query(db, "INSERT INTO Bans (`SteamID`,`Nick`,`Banner`,`Comment`,`Bantime`)VALUES ('".. t:SteamID() .."','".. t:Nick() .."','".. ply:Nick() .."','"..Reason.."',".. Bantime ..")");
		if (!isok) then
			print(tostring(error) .. "\n");
		end
		if (tonumber(Time)*60) >= 1440 then
			BanMark(t)
		end
		local succ, error = mysql.disconnect(db)
		if (not succ) then
			print( error );
		end
		t:Kick("Banned: "..Reason)
	end
end

function GamewebUnban(ply,steam)
	if IsGamewebAdmin(ply) == true then
		local db, error2 = mysql.connect(host, username, password, db)  
		if (db == 0) then print(tostring(error2) .. "\n") return end  
			insert, isok, error = mysql.query(db, "DELETE FROM bans WHERE SteamID='".. steam  .."'");
		if (!isok) then
			print(tostring(error) .. "\n");
		end
		local succ, error = mysql.disconnect(db)
		if (not succ) then
			print( error );
		end
	end
end
BanTable()